package com.dm.cloud.file.conf;

import com.dm.cloud.core.service.user.CurrentUserService;
import com.dm.cloud.oauthlib.conf.AuthInteceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfigure implements WebMvcConfigurer {
    @Autowired
    private CurrentUserService currentUserService;

    @Autowired
    private LocalUtil localUtil;

    @Value("${server.servlet.context-path:}")
    private String servletContentPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(
                new AuthInteceptor(currentUserService,servletContentPath)
                        .hasLogin("/apis/v1/*/pageList")
                        .hasLogin("/apis/v1/*/detail")
                        .hasLogin("/apis/v1/*/insert")
                        .hasLogin("/apis/v1/*/update")
                        .hasLogin("/apis/v1/*/deleteById")
                        .hasLogin("/apis/v1/*/deleteBatchIds")
                        .hasLogin("/apis/v1/*/idsList")
        ).addPathPatterns("/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("file:"+localUtil.getFileDir().getAbsolutePath()+"/");
    }

}
